/*  
 *  Log.cs - Simple debug logging class.
 *  Copyright(c) 2007 Adam W Adair
 *
 *  This file is part of Adam's Chess Engine (ACE).
 *
 *  ACE is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *   
 *  ACE is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ACE; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ACE
{
    /// <summary>
    /// Log provides simple file logging ability for ACE.
    /// </summary>
    class Log
    {
        [Flags]
        public enum LogLevel {NO_LOG=0, LOG_ERROR=1, LOG_WARNING=2, LOG_INFO=4, LOG_DEBUG=8}
        private static LogLevel defaultLogLevel;
        private static Log defaultLogger;
        private LogLevel m_level;
        private string m_pathname;

        static Log()
        {
            defaultLogLevel = LogLevel.NO_LOG;
            defaultLogger = null;
        }

        public static Log DefaultLog
        {
            get
            {
                if (defaultLogger == null) defaultLogger = new Log();
                return defaultLogger;
            }
            set
            {
                defaultLogger = value;
            }
        }

        public Log()
        {
            m_level = defaultLogLevel;
            m_pathname = "ace.log";
        }

        public Log(LogLevel level)
        {
            m_level = level;
            m_pathname = "ace.log";
        }

        /// <summary>
        /// Sets the default log level for loggers.
        /// </summary>
        public static LogLevel DefaultLogLevel
        {
            get{return defaultLogLevel;}
            set{defaultLogLevel=value;}
        }

        public string Pathname
        {
            get{
                return m_pathname;
            }
            set{
                m_pathname = value;
            }
        }

        public LogLevel Level
        {
            get{ return m_level; }
            set{ m_level = value; }
        }

        public void LogError(string message)
        {
            if ((Level & LogLevel.LOG_ERROR)!=0)
                LogMessage("ERROR", message);
        }

        public void LogException(Exception e)
        {
            if ((Level & LogLevel.LOG_ERROR)!=0)
                LogMessage("ERROR", e.Message);
        }

        public void LogWarning(string message)
        {
            if ((Level & LogLevel.LOG_WARNING)!=0)
                LogMessage("WARNING", message);
        }

        public void LogInfo(string message)
        {
            if ((Level & LogLevel.LOG_INFO)!=0)
                LogMessage("INFO", message);

        }

        public void LogDebug(string message)
        {
            if ((Level & LogLevel.LOG_DEBUG)!=0)
                LogMessage("DEBUG", message);
        }

        private void LogMessage(string type, string message)
        {
            try
            {
                string sLogFormat = type + " " + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
                StreamWriter sw = new StreamWriter(Pathname, true);
                sw.WriteLine(sLogFormat + message);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }

}
